layui.use(['form', 'layedit','jquery','laydate','tableSelect'], function() {
    var form = layui.form
        , layer = layui.layer
        , layedit = layui.layedit
        , laydate = layui.laydate
        ,$=layui.$;
    var tableSelect = layui.tableSelect;

    $.ajaxSetup({
        async: false
    })

    $('#close').on("click",function (){
        layerClose()
    })

    //日期
    laydate.render({
        elem: '#inStorageGetTime',
        type: 'datetime'
    });

    //日期
    laydate.render({
        elem: '#inStorageOutTime',
        type: 'datetime'
    });
    //仓库名
    tableSelect.render({
        elem: '#storageName',	//定义输入框input对象 必填
        checkedKey: 'storageId', //表格的唯一建值，非常重要，影响到选中状态 必填
        searchKey: 'keyWord',	//搜索输入框的name值 默认keyword
        searchPlaceholder: '关键词搜索',	//搜索输入框的提示文字 默认关键词搜索
        table: {	//定义表格参数，与LAYUI的TABLE模块一致，只是无需再定义表格elem
            url:'/logisticsStorage/queryAll',
            cols: [[
                {type: 'radio', fixed: 'left'}
                , {field: 'storageId', title: '仓库ID', width: 150, sort: true, fixed: 'left'}
                , {field: 'storageName', title: '仓库名', width: 150, sort: true}
                , {field: 'storagePrincipal', title: '负责人', width: 150, sort: true}
                , {field: 'storageTel', title: '联系电话', width: 150, sort: true}
                , {field: 'storageAddress', title: '仓库地址', width: 200}
            ]]
        },
        done: function (elem, data) {
            //选择完后的回调，包含2个返回值 elem:返回之前input对象；data:表格返回的选中的数据 []
            //拿到data[]后 就按照业务需求做想做的事情啦~比如加个隐藏域放ID...
            var NEWJSON = []
            var NEWJSON1 = []
            for (let i = 0; i < data.data.length; i++) {
                NEWJSON.push(data.data[i].storageName);
                NEWJSON1.push(data.data[i].storageId);
            }
            $("#storageName").val(NEWJSON.join(","));
            $("#storageId").val(NEWJSON1.join(","));
        }
    })
    // 运单编号
    tableSelect.render({
        elem: '#waybillId',	//定义输入框input对象 必填
        checkedKey: 'waybillId', //表格的唯一建值，非常重要，影响到选中状态 必填
        searchKey: 'waybillId',	//搜索输入框的name值 默认keyword
        searchPlaceholder: '根据运单编号搜索',	//搜索输入框的提示文字 默认关键词搜索
        table: {	//定义表格参数，与LAYUI的TABLE模块一致，只是无需再定义表格elem
            url:'/logisticsWaybill/select',
            cols: [[
                {type: 'radio', fixed: 'left'}
                , {field: 'waybillId', title: '运单编号', width: 240, sort: true, fixed: 'left'}
                , {field: 'waybillSign', title: '签收人', width: 150, sort: true}
                , {field: 'waybillRemark', title: '备注', width: 200}
            ]]
        },
        done: function (elem, data) {
            //选择完后的回调，包含2个返回值 elem:返回之前input对象；data:表格返回的选中的数据 []
            //拿到data[]后 就按照业务需求做想做的事情啦~比如加个隐藏域放ID...
            var NEWJSON = []
            var NEWJSON1 = []
            var NEWJSON2 = []
            for (let i = 0; i < data.data.length; i++) {
                NEWJSON.push(data.data[i].waybillId);
                NEWJSON1.push(data.data[i].waybillDeparture);
                NEWJSON2.push(data.data[i].waybillBourn);
            }
            $("#waybillId").val(NEWJSON.join(","));
            $("#inStoragePrevious").val(NEWJSON1.join(","));
            $("#inStorageNext").val(NEWJSON2.join(","));
        }
    })

    //监听提交
    form.on('submit(tijiao)', function(data){
        let url="/logisticsInStorage/addOrUpdate";
        let data1=data.field;
        $.post(url,data1,function (resp){
            if (resp.code===0){
                layerClose();
                layer.msg(resp.msg);
                parent.table.reload('inStorage');
                parent.layer.msg("操作成功");
            }else {
                layer.msg(resp.msg);
            }
        },"json");
        return false;
    });

    let id=getQueryString("id");
    if (id!=null){
        let url="/logisticsInStorage/showUpdate";
        let data1={id:id};
        console.info(id);
        $.post(url,data1,function (resp){
            if (resp.code===0) {
                form.val('storage1', resp.data);
                console.info(resp.data)
                $("#storageName").attr("ts-selected",resp.data.storageId);
                $("#storageName").val(resp.storageName);
                $("#waybillId").attr("ts-selected",resp.data.waybillId);
                $("#waybillId").val(resp.data.waybillId);
                form.render();
            }
        },"json")
    }

})

/**
 得到地址栏的参数
 */
function getQueryString(name){
    var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if(r!=null)return  unescape(r[2]); return null;
}

